package validator;

import exception.ParamCheckFailureException;

/**
 * 数值类型参数校验器
 * <p>
 * 说明：此类在构造时就对数值类型和null进行了判断，只要构造成功方法调用便是安全的。
 *
 * @Author 小白龙
 * @Date 2021-01-05
 * @Version 1.0
 */
public class NumValidator extends AbstractNumberValidator<Long> {

    private String paramName;
    private Long paramValue;

    /**
     * 必须不为null的构造器
     *
     * @param paramValue
     * @param paramName
     */
    public NumValidator(String paramValue, String paramName) throws ParamCheckFailureException {
        this(parseLong(paramValue, paramName), paramName);
    }

    /**
     * 必须不为null的构造器
     *
     * @param paramValue
     * @param paramName
     */
    public NumValidator(Long paramValue, String paramName) throws ParamCheckFailureException {
        this.paramValue = paramValue;
        this.paramName = paramName;
    }

    @Override
    protected String getParamName() {
        return paramName;
    }

    @Override
    protected Long getParamValue() {
        return paramValue;
    }


    @Override
    public NumValidator min(Long min, String errorMessage) throws ParamCheckFailureException {
        return null;
    }

    @Override
    public NumValidator max(Long max, String errorMessage) throws ParamCheckFailureException {
        return null;
    }


    @Override
    public NumValidator gtZero(String errorMessage) throws ParamCheckFailureException {
        if (paramValue.compareTo(0L) <= 0) {
            throw createPcfException(paramName + errorMessage);
        }
        return this;
    }

    @Override
    public NumValidator gtOrEqZero(String errorMessage) throws ParamCheckFailureException {
        if (paramValue.compareTo(0L) < 0) {
            throw createPcfException(paramName + errorMessage);
        }
        return this;
    }

    @Override
    public NumValidator ltZero(String errorMessage) throws ParamCheckFailureException {
        if (paramValue.compareTo(0L) >= 0) {
            throw createPcfException(paramName + errorMessage);
        }
        return this;
    }

    @Override
    public NumValidator ltOrEqZero(String errorMessage) throws ParamCheckFailureException {
        if (paramValue.compareTo(0L) >= 0) {
            throw createPcfException(paramName + errorMessage);
        }
        return this;
    }

    /**
     * String 转类型改变异常类型
     *
     * @param value
     * @param paramName
     * @return
     * @throws ParamCheckFailureException
     */
    private static Long checkNotNull(Long value, String paramName) throws ParamCheckFailureException {
        notNull(value, strConcat(paramName, ERROR_MESSAGE_NOT_NULL));
        return value;
    }

    /**
     * String 转类型改变异常类型
     *
     * @param value
     * @param paramName
     * @return
     * @throws ParamCheckFailureException
     */
    private static Long parseLong(String value, String paramName) throws ParamCheckFailureException {
        try {
            return Long.valueOf(value);
        } catch (NumberFormatException nfe) {
            throw createPcfException(paramName + "必须为整数");
        }
    }
}
